package com.ruoyi.vqms.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringTools;
import com.ruoyi.system.domain.led.CfLinXinLed;
import com.ruoyi.system.domain.led.CfLinXinLedArea;
import com.ruoyi.system.domain.led.CfLinXinLedProgram;
import com.ruoyi.vqms.domain.VQMSLed;
import com.ruoyi.vqms.domain.VQMSQueue;
import com.ruoyi.vqms.domain.VQMSQueueArea;
import com.ruoyi.vqms.mapper.VQMSLedMapper;
import com.ruoyi.vqms.mapper.VQMSQueueAreaMapper;
import com.ruoyi.vqms.mapper.VQMSQueueMapper;
import com.ruoyi.vqms.service.IVQMSLedService;
import com.ruoyi.vqms.service.socket.CfLogisticsLedClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 访客（司机） 服务层实现
 */
@Service
public class VQMSLedServiceImpl implements IVQMSLedService {
    // 用户保存用户id与通道的Map对象
    public static Map<String, CfLogisticsLedClient> logisticsLedClientMap = new HashMap<>();
    public static final String DEVICE_LINK_IP = "logistics_ledDevice_link_ip_";
    @Autowired
    private VQMSLedMapper vqmsLedMapper;
    @Autowired
    private VQMSQueueMapper vqmsQueueMapper;
    @Autowired
    private VQMSQueueAreaMapper vqmsQueueAreaMapper;
    @Autowired
    private RedisCache redisCache;

    @Override
    public List<VQMSLed> selectList(VQMSLed vqmsLed) {
        return vqmsLedMapper.selectList(vqmsLed);
    }

    @Override
    public List<VQMSLed> ledList(Long areaId) {
        return vqmsLedMapper.ledList(areaId);
    }

    @Override
    public int add(VQMSLed vqmsLed) {
        return vqmsLedMapper.add(vqmsLed);
    }

    @Override
    public VQMSLed getById(Long id) {
        return vqmsLedMapper.getById(id);
    }

    @Override
    public int update(VQMSLed vqmsLed) {
        return vqmsLedMapper.update(vqmsLed);
    }

    @Override
    public int delete(Long[] ids) {
        return vqmsLedMapper.delete(ids);
    }

    @Override
    public void handleLink(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        byte[] data = new byte[buf.readableBytes()];
        buf.readBytes(data);
        String message = StringTools.bytesToString16(data);

        CfLogisticsLedClient cfLogisticsLedClient = new CfLogisticsLedClient();
        cfLogisticsLedClient.setChannel(ctx.channel());
        cfLogisticsLedClient.setFrameHeader(message.substring(0, 8));
        cfLogisticsLedClient.setAddress(message.substring(8, 10));
        cfLogisticsLedClient.setMarker(message.substring(10, 12));
        cfLogisticsLedClient.setOpcode(message.substring(12, 16));
        cfLogisticsLedClient.setReservedCode(message.substring(16, 20));
        cfLogisticsLedClient.setFrameNumber(message.substring(20, 28));
        cfLogisticsLedClient.setTotalLength(message.substring(28, 36));
        cfLogisticsLedClient.setFrameLength(message.substring(36, 40));
        cfLogisticsLedClient.setSerialNumber(message.substring(40, message.length() - 8));
        cfLogisticsLedClient.setEndOfFrame(message.substring(message.length() - 8, message.length()));

//        System.out.println("led心跳信息---->" + cfLogisticsLedClient + "    ctx对象---》" + ctx);
        ByteBuf returnProtocolBuf = Unpooled.buffer();
        if (cfLogisticsLedClient.getOpcode().equals("00D8")) {        //心跳包操作码： 0xD800
            cfLogisticsLedClient.setHeartBeatContents(message);
            //说明是正常心跳
            CfLogisticsLedClient logisticsLedClient = logisticsLedClientMap.get(cfLogisticsLedClient.getSerialNumber());
            if (logisticsLedClient == null || !logisticsLedClient.getChannel().id().asLongText().equals(ctx.channel().id().asLongText())) {
                logisticsLedClientMap.put(cfLogisticsLedClient.getSerialNumber(), cfLogisticsLedClient);
                //将设备在线设备ip记录到redis中去
                redisCache.setCacheObject(VQMSLedServiceImpl.DEVICE_LINK_IP + cfLogisticsLedClient.getSerialNumber(), InetAddress.getLocalHost().getHostAddress());
                redisCache.expire(VQMSLedServiceImpl.DEVICE_LINK_IP + cfLogisticsLedClient.getSerialNumber(), 365, TimeUnit.DAYS);
            }
            System.out.println("回复心跳信息---->" + message);
            returnProtocolBuf.writeBytes(StringTools.hexStringToBytes(message));
//            ctx.channel().writeAndFlush(returnProtocolBuf);

            // 使用ChannelFuture监听写操作结果
            ChannelFuture future = ctx.channel().writeAndFlush(returnProtocolBuf);
            future.addListener(f -> {
                if (!f.isSuccess()) {
                    System.out.println("心跳回复失败: " + f.cause());
                    ctx.close();
                }
            });
        }
    }

    // 添加定时任务检查设备连接状态
    @Scheduled(fixedRate = 30000) // 每30秒执行一次
    public void checkDeviceConnections() {
        long now = System.currentTimeMillis();

        logisticsLedClientMap.forEach((serialNumber, client) -> {
            Channel channel = client.getChannel();
            if (!channel.isActive()) {
                System.out.println("检测到非活跃连接，移除设备: " + serialNumber);
                logisticsLedClientMap.remove(serialNumber);
//                redisCache.delete(DEVICE_LINK_IP + serialNumber);
            }  // 可以添加额外的连接检查逻辑

        });
    }

    @Override
    public String makeTcpData(String serialNumber, CfLinXinLed cfLinXinLed) throws Exception {
        CfLogisticsLedClient cfLogisticsLedClient = logisticsLedClientMap.get(serialNumber);
        if (cfLogisticsLedClient == null) {
            throw new Exception("指定序列号[" + serialNumber + "]设备不在线");
        }
        //【显示数据属性】
        String datas = "75757575757575757575757575757575";  //固定
        datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getScreenWidth()), 4), 2); //显示屏宽，横向点数
        datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getScreenHigh()), 4), 2); //显示屏高，纵向点数
        datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getPrimaryColor()), 2); //1—单基色，2—双基色，3—三基色
        datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getGrayScale()), 2); //灰度等级 bit 数（无灰度为 1，4 级灰度为 2，8 级灰度为 3，16 级灰度为 4，32 级灰度时为 5）
        datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getNumberOfPrograms()), 2); //存入所在位置的节目个数(两者之和需小于 256)
        datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getPlaybackMode()), 2); //0---节目存入 Flash，如之前有 RAM 节目存在时是混合一起播放1---节目存入 RAM，如之前有 Flash 节目存在时是混合一起播放2—节目存入 Flash 且仅播放 Flash 节目3—节目存入 RAM 且仅播放 RAM 节目
        datas += "0000000000000000"; //全为 0x00
        datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getTotalSizeOfProgramData()), 8), 2); //整个节目数据的总大小
        datas += "0000000000000000"; //全为 0x00
        //【节目属性】
        for (CfLinXinLedProgram cfLinXinLedProgram : cfLinXinLed.getCfLinXinLedProgramList()) {
            datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedProgram.getProgramNumber()), 2); //节目编号，从 0 开始，从小到大排，不能跳跃
            datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedProgram.getProgramDataBlockSize()), 8), 2); //节目数据块的大小
            datas += "02"; //固定为 0x02
            datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedProgram.getAreaSize()), 2); //节目中区域个数
            datas += StringTools.reverseOrder(StringTools.preMakeUp(Integer.toHexString(cfLinXinLedProgram.getProgramPlayMode()), "0", 4), 2); //0—单节目循环播放，非 0—单节目定长播放秒数
            datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedProgram.getCyclePlaybackTimes()), 2); //单节目循环播放时的次数
            datas += "00000000000000000000000000000000"; //全为 0x00
            //【区域属性】
            for (CfLinXinLedArea cfLinXinLedArea : cfLinXinLedProgram.getCfLinXinLedAreaList()) {
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaNumber()), 2); //区域编号
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaDataBlockSize()), 8), 2); //区域数据块的大小
                datas += "E5"; //固定为 0Xe5
                datas += "01"; //固定为 0x01
                datas += "01"; //固定为 0x01
                datas += "00000000"; //全为 0x00
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaX()), 4), 2); //区域开始 x 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaY()), 4), 2); //区域开始 y 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaXX() - 1), 4), 2); //区域开始 xx 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaYY() - 1), 4), 2); //区域开始 yy 坐标
                datas += "000000000000000000000000"; //全为 0x00
                datas += "02"; //固定为 0x02
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getAreaDataBlockSize() - 32), 8), 2); //区域数据块的大小 减去 32
                datas += "0E"; //固定为 0x0E
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getNumberOfPages()), 4), 2); //区域中的页数
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontSizeWidth()), 2); //字体宽
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontSizeHigh()), 2); //字体高
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontColor()), 2); //字体颜色值
                datas += "03"; //固定为 0x03
                datas += "000000000000000000000000"; //全为 0x00
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontInMode()), 2); //当前页进场特效
                datas += "FF"; //固定为 0x00
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontInSpeed()), 2); //当前页进场移动速度
                datas += "FF"; //固定为 0x00
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontResidenceTime()), 4), 2); //当前页停留时间
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getPageAreaX()), 4), 2); //当前页数据有效区域 x 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getPageAreaY()), 4), 2); //当前页数据有效区域 y 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getPageAreaXX() - 1), 4), 2); //当前页数据有效区域 xx 坐标
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getPageAreaYY() - 1), 4), 2); //当前页数据有效区域 yy 坐标
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontMoveStep()), 2); //连续移动时的步长
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontRowSpacing()), 2); //当前页的行间距，两行之间的点数
                datas += StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getPageDataSize()), 4), 2); //本页的数据长度
                datas += StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLedArea.getFontWordSpacing()), 2); //当前页的字间距
                datas += "FF"; //固定为 0x00
                datas += cfLinXinLedArea.getHexContents();  //文字数据内容
            }
        }
        cfLogisticsLedClient.setTotalLength(StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getTotalSizeOfProgramData()), 8), 2));   //总长度
        cfLogisticsLedClient.setFrameLength(StringTools.reverseOrder(StringTools.preMakeUp("0", Integer.toHexString(cfLinXinLed.getTotalSizeOfProgramData()), 4), 2));   //帧长度
        //帧头 + 地址 + 标识（01）+ 操作码（00DA）+ 保留码 + 帧序号 + 总长度 + 帧长度 + 要展示的数据 + 帧尾
        return cfLogisticsLedClient.getFrameHeader() + cfLogisticsLedClient.getAddress()
                + "01" + "00DA" + cfLogisticsLedClient.getReservedCode()
                + cfLogisticsLedClient.getFrameNumber() + cfLogisticsLedClient.getTotalLength()
                + cfLogisticsLedClient.getFrameLength() + datas
                + cfLogisticsLedClient.getEndOfFrame();
    }

    public Integer[] size(String date) throws Exception {
//        String hexContents = StringTools.str2HexStr(date,"GB18030");
        //设置本页数据长度，本系统开发所有都只使用一页，暂时不考虑多页
        Integer pageDataSize = StringTools.strToHexString(date).length() / 2;
        //主板区域区块大小 76是固定的
        Integer areaDataBlockSize = (76 + pageDataSize);

        //节目数据块的大小（包含节目属性长度 26）
        Integer programDataBlockSize = 26 + areaDataBlockSize;

        //整个节目数据的总大小（包含显示数据属性长度 44）
        Integer totalSizeOfProgramData = 44 + programDataBlockSize;

        return new Integer[]{areaDataBlockSize, programDataBlockSize, totalSizeOfProgramData, pageDataSize};
    }

    public String hexStr(String text) throws Exception {
        String datas = StringTools.str2HexStr(text, "GB18030");
        return datas;
    }


    @Override
    public void sendTcpData(String serialNumber, CfLinXinLed cfLinXinLed) throws Exception {
        CfLogisticsLedClient cfLogisticsLedClient = logisticsLedClientMap.get(serialNumber);    //获取设备客户端对象
        if (cfLogisticsLedClient == null) {
            throw new Exception("指定序列号[" + serialNumber + "]设备不在线");
        }
        String tcpData = makeTcpData(serialNumber, cfLinXinLed).toUpperCase();  //生成TCP数据
        ByteBuf returnProtocolBuf = Unpooled.buffer();                          //创建ByteBuf缓冲区
        returnProtocolBuf.writeBytes(StringTools.hexStringToBytes(tcpData));    //将TCP数据写入缓冲区
        cfLogisticsLedClient.getChannel().writeAndFlush(returnProtocolBuf);     //将缓冲区TCP数据发送到LED设备
    }

    @Override
    public void flushLesScreenData(Long areaId) throws Exception {
        //排除采购A、BC仓之外的队列ID
        if (areaId == 1 || areaId == 41) {
            //查询所有当前排队队列ID的所有车辆
            VQMSQueue vqmsQueue1 = new VQMSQueue();
            vqmsQueue1.setAreaId(areaId);
            //获取当日排队车辆
            List<VQMSQueue> vqmsQueueList = vqmsQueueMapper.getQueueList(areaId);
            int size = vqmsQueueList.size();
            Long e = 2L;
            //遍历排队，将加急情况放到车牌后面
            for (VQMSQueue vqmsQueue : vqmsQueueList) {
                if (vqmsQueue.getExpedited() == e) {      //加急
                    vqmsQueue.setLicensePlate(vqmsQueue.getLicensePlate() + "(急)");
                }
            }
            System.out.println("排队车辆--》" + vqmsQueueList);

            //查询排队队列名称，预防size为0的情况
            VQMSQueueArea vqmsQueueArea1 = vqmsQueueAreaMapper.getById(areaId);

            //查询LED设备信息，然后根据从上往下的顺序重新制作展示信息（172.16.206.4、172.16.206.3、172.16.206.2）
            List<VQMSLed> ledList = vqmsLedMapper.ledList(areaId);
            String palyContents = "";
            String serialNumber = "";
            String spaceInterval = "";

            if (size < 1) {
                //先将原有排队数据清空
                serialNumber = ledList.get(0).getSerialNumber();     //第一块板
                palyContents = vqmsQueueArea1.getName();
                //【文本列表】
                ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                //装配内码区域
                cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 80, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                palyContents = "暂无排队车辆";
                cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 8, 24, 256, 48, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                //节目1
                CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                sendTcpData(serialNumber, cfLinXinLed);

                serialNumber = ledList.get(1).getSerialNumber();     //第二块板
                palyContents = " ";
                //【文本列表】
                ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList1 = new ArrayList<>();
                //装配内码区域
                cfLinXinLedAreaArrayList1.add(new CfLinXinLedArea(0, 16, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                cfLinXinLedAreaArrayList1.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                //节目1
                CfLinXinLedProgram cfLinXinLedProgram1 = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList1);
                ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList1 = new ArrayList<>();
                cfLinXinLedProgramArrayList1.add(cfLinXinLedProgram1);
                CfLinXinLed cfLinXinLed1 = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList1);
                sendTcpData(serialNumber, cfLinXinLed1);

                serialNumber = ledList.get(2).getSerialNumber();     //第三块板
                sendTcpData(serialNumber, cfLinXinLed1);
            } else {
                //第一块板
                if (size >= 1) {
                    ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                    ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                    serialNumber = ledList.get(0).getSerialNumber();
                    //装排队队列名称
                    palyContents = vqmsQueueArea1.getName();
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 80, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    //装第一块板第二行排队车辆信息
                    palyContents = StringTools.preMakeUp("0", +vqmsQueueList.get(0).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(0).getLicensePlate();
                    if (size >= 6) {
                        if (vqmsQueueList.get(0).getExpedited() == e) {     //第一条排队加急
                            spaceInterval = "  ";
                        } else {
                            spaceInterval = "     ";
                        }
                        palyContents += spaceInterval + StringTools.preMakeUp("0", +vqmsQueueList.get(6 - 1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(6 - 1).getLicensePlate();
                    }
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 0, 4, 0, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    //节目1
                    CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                    cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                    CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                    sendTcpData(serialNumber, cfLinXinLed);
                }

                //第二块板
                if (size >= 2) {
                    ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                    ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                    serialNumber = ledList.get(1).getSerialNumber();
                    palyContents = StringTools.preMakeUp("0", +vqmsQueueList.get(1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(1).getLicensePlate();
                    if (size >= 7) {
                        if (vqmsQueueList.get(1).getExpedited() == e) {     //第二条排队加急
                            spaceInterval = "  ";
                        } else {
                            spaceInterval = "     ";
                        }
                        palyContents += spaceInterval + StringTools.preMakeUp("0", +vqmsQueueList.get(7 - 1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(7 - 1).getLicensePlate();
                    }
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 16, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));

                    if (size >= 3) {
                        palyContents = StringTools.preMakeUp("0", +vqmsQueueList.get(2).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(2).getLicensePlate();
                        if (size >= 8) {
                            if (vqmsQueueList.get(2).getExpedited() == e) {     //第三条排队加急
                                spaceInterval = "  ";
                            } else {
                                spaceInterval = "     ";
                            }
                            palyContents += spaceInterval + StringTools.preMakeUp("0", +vqmsQueueList.get(8 - 1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(8 - 1).getLicensePlate();
                        }
                        cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 0, 4, 0, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    }
                    //节目1
                    CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                    cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                    CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                    sendTcpData(serialNumber, cfLinXinLed);
                } else {
                    ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                    ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                    serialNumber = ledList.get(1).getSerialNumber();
                    palyContents = " ";
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 16, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 0, 4, 0, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    //节目1
                    CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                    cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                    CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                    sendTcpData(serialNumber, cfLinXinLed);
                }

                //第三块板
                if (size >= 4) {
                    ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                    ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                    serialNumber = ledList.get(2).getSerialNumber();
                    palyContents = StringTools.preMakeUp("0", +vqmsQueueList.get(3).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(3).getLicensePlate();
                    if (size >= 9) {
                        if (vqmsQueueList.get(3).getExpedited() == e) {     //第四条排队加急
                            spaceInterval = "  ";
                        } else {
                            spaceInterval = "     ";
                        }
                        palyContents += spaceInterval + StringTools.preMakeUp("0", +vqmsQueueList.get(9 - 1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(9 - 1).getLicensePlate();
                    }
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 16, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));

                    if (size >= 5) {
                        palyContents = StringTools.preMakeUp("0", +vqmsQueueList.get(4).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(4).getLicensePlate();
                        if (size >= 10) {
                            if (vqmsQueueList.get(4).getExpedited() == e) {     //第五条排队加急
                                spaceInterval = "  ";
                            } else {
                                spaceInterval = "     ";
                            }
                            palyContents += spaceInterval + StringTools.preMakeUp("0", +vqmsQueueList.get(10 - 1).getQueueIndex() + "", 2) + " " + vqmsQueueList.get(10 - 1).getLicensePlate();
                        }
                        cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 0, 4, 0, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    }
                    //节目1
                    CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                    cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                    CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                    sendTcpData(serialNumber, cfLinXinLed);

                } else {
                    ArrayList<CfLinXinLedArea> cfLinXinLedAreaArrayList = new ArrayList<>();
                    ArrayList<CfLinXinLedProgram> cfLinXinLedProgramArrayList = new ArrayList<>();
                    serialNumber = ledList.get(2).getSerialNumber();
                    palyContents = " ";
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(0, 16, 0, 256, 24, 1, 16, 16, 0, 1, 4, 3, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    cfLinXinLedAreaArrayList.add(new CfLinXinLedArea(1, 16, 24, 256, 48, 1, 16, 16, 0, 0, 4, 0, 0, 0, 256, 48, 1, 9, 0, palyContents));
                    //节目1
                    CfLinXinLedProgram cfLinXinLedProgram = new CfLinXinLedProgram(0, 0, 1, cfLinXinLedAreaArrayList);
                    cfLinXinLedProgramArrayList.add(cfLinXinLedProgram);
                    CfLinXinLed cfLinXinLed = new CfLinXinLed(256, 48, 1, 5, 2, cfLinXinLedProgramArrayList);
                    sendTcpData(serialNumber, cfLinXinLed);
                }
            }
        } else {
            System.out.println("排队队列ID无效："+areaId);
        }
    }

}
